<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>选号</title>
</head>
<body>
    <button type="button" id="btnGenerate" onclick="generate()">点击选号</button>
    <div id="divContent"></div>
</body>
<script>
    function generate() {
        let btnGenerate = document.querySelector('#btnGenerate');
        btnGenerate.disabled = true
        btnGenerate.innerHTML = '生成中，请稍候'
        let divContent = document.querySelector('#divContent')
        divContent.innerHTML = ''

        setTimeout(() => {
            let totalStartTime = Date.now()
            let blueExistedSet = new Set()
            let totalCount = 0
            while (true) {
                let objects = getBingoNum()
                let bitRedArr = objects[0]
                let bitBlue = objects[1]
                let count = objects[2]
                let p = document.createElement('p')
                p.innerHTML = `红：${bitRedArr}, 蓝：${bitBlue} 期数：${count}`
                divContent.appendChild(p)
                blueExistedSet.add(bitBlue)
                totalCount++
                if (blueExistedSet.size === 16) {
                    break
                }
            }
            let totalEndTime = Date.now()
            let p = document.createElement('p');
            p.innerHTML = '一共选出' + totalCount + '注'
            divContent.appendChild(p)
            p = document.createElement('p')
            p.innerHTML = '总耗时：' + (totalEndTime - totalStartTime) / 1000 + '秒.'
            divContent.appendChild(p)
            btnGenerate.disabled = false
            btnGenerate.innerHTML = '重新生成'
        }, 0)
    }

    function getBingoNum() {
        let count = 0
        let bitRedArr
        let bitBlue
        while (true) {
            count++
            bitRedArr = []
            for (let i = 0; i < 6; i++) {
                let num = 0
                while (true) {
                    num = Math.floor(Math.random() * 33 + 1)
                    if (bitRedArr.indexOf(num) === -1) {
                        break
                    }
                }
                bitRedArr.push(num)
            }
            bitBlue = Math.floor(Math.random() * 16 + 1)

            let resultRedArr = []
            for (let i = 0; i < 6; i++) {
                let num = 0
                while (true) {
                    num = Math.floor(Math.random() * 33 + 1)
                    if (resultRedArr.indexOf(num) === -1) {
                        break
                    }
                }
                resultRedArr.push(num)
            }
            let resultBlue = Math.floor(Math.random() * 16 + 1)

            if (bingo(bitRedArr, bitBlue, resultRedArr, resultBlue)) {
                bitRedArr.sort()
                break
            }
        }
        return [ bitRedArr, bitBlue, count ]
    }

    function bingo(bitRedArr, bitBlue, resultRedArr, resultBlue) {
        if (bitBlue !== resultBlue) {
            return false
        }
        for (let integer of bitRedArr) {
            if (resultRedArr.indexOf(integer) === -1) {
                return false
            }
        }
        return true
    }
</script>
</html>